package com.sailfish.springbootdemo.dao.db1;

import com.sailfish.springbootdemo.common.DB1TableName;
import com.sailfish.springbootdemo.pojo.db1.PcbaDeviceParamType;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

public interface PcbaDeviceParamTypeDao {

    /**
     * 根据 设备类型id 查询
     *
     * @param pcbaDeviceTypeId
     * @return
     */
    @Results(id = "pcbaDeviceParamTypeResultMap", value = {
            @Result(column = "pcba_device_param_type_id", property = "pcbaDeviceParamTypeId", jdbcType = JdbcType.INTEGER),
            @Result(column = "pcba_device_param_type_name", property = "pcbaDeviceParamTypeName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "sort", property = "sort", jdbcType = JdbcType.INTEGER),
            @Result(column = "status", property = "status", jdbcType = JdbcType.INTEGER),
            @Result(column = "create_time", property = "createTime", jdbcType = JdbcType.DATETIMEOFFSET),
            @Result(column = "pcba_device_type_id", property = "pcbaDeviceTypeId", jdbcType = JdbcType.INTEGER),
    })
    @Select("<script>" +
            " select * from " + DB1TableName.PCBA_DEVICE_PARAM_TYPE + " where 1 = 1 " +
            "<if test='pcbaDeviceTypeId != null and pcbaDeviceTypeId != \"\" '>" +
            " and pcba_device_type_id = #{pcbaDeviceTypeId}" +
            "</if>" +
            "<if test='searchValue != null and searchValue != \"\" '>" +
            " and (pcba_device_param_type_name like CONCAT('%',#{searchValue},'%'))" +
            "</if>" +
            " order by sort" +
            "</script>")
    List<PcbaDeviceParamType> getPcbaDeviceParamTypeByPcbaDeviceTypeId(@Param("pcbaDeviceTypeId") Integer pcbaDeviceTypeId, @Param("searchValue") String searchValue);

    /**
     * 根据id查询
     *
     * @param pcbaDeviceParamTypeId
     * @return
     */
    @ResultMap(value = "pcbaDeviceParamTypeResultMap")
    @Select("select * from " + DB1TableName.PCBA_DEVICE_PARAM_TYPE + " where pcba_device_param_type_id = #{pcbaDeviceParamTypeId}")
    PcbaDeviceParamType getPcbaDeviceParamTypeById(@Param("pcbaDeviceParamTypeId") Integer pcbaDeviceParamTypeId);

    /**
     * 判断有没有重复的
     */
    @Select("<script>" +
            "select * from " + DB1TableName.PCBA_DEVICE_PARAM_TYPE + " where pcba_device_param_type_name = #{pcbaDeviceParamTypeName} and pcba_device_type_id = #{pcbaDeviceTypeId} " +
            "<if test='pcbaDeviceParamTypeId != null and pcbaDeviceParamTypeId != \"\" '>" +
            " and pcba_device_param_type_id != #{pcbaDeviceParamTypeId}" +
            "</if>" +
            "</script>")
    @ResultMap(value = "pcbaDeviceParamTypeResultMap")
    List<PcbaDeviceParamType> judgeUniquePcbaDeviceParamType(@Param("pcbaDeviceParamTypeId") Integer pcbaDeviceParamTypeId, @Param("pcbaDeviceTypeId") Integer pcbaDeviceTypeId, @Param("pcbaDeviceParamTypeName") String pcbaDeviceParamTypeName);

    /**
     * 删除
     *
     * @param pcbaDeviceParamTypeId
     * @return
     */
    @Delete("delete from " + DB1TableName.PCBA_DEVICE_PARAM_TYPE + " where pcba_device_param_type_id = #{pcbaDeviceParamTypeId}")
    Integer deletePcbaDeviceParamType(@Param("pcbaDeviceParamTypeId") Integer pcbaDeviceParamTypeId);

    /**
     * 新增
     *
     * @param pcbaDeviceParamType
     * @return
     */
    @Insert("insert into " + DB1TableName.PCBA_DEVICE_PARAM_TYPE + "(pcba_device_param_type_name,status,sort,create_time,pcba_device_type_id) values " +
            "(#{pcbaDeviceParamType.pcbaDeviceParamTypeName},#{pcbaDeviceParamType.status},#{pcbaDeviceParamType.sort},#{pcbaDeviceParamType.createTime},#{pcbaDeviceParamType.pcbaDeviceTypeId})")
    Integer addPcbaDeviceParamType(@Param("pcbaDeviceParamType") PcbaDeviceParamType pcbaDeviceParamType);

    /**
     * 更新
     *
     * @param pcbaDeviceParamType
     * @return
     */
    @Update("<script>" +
            "update " + DB1TableName.PCBA_DEVICE_PARAM_TYPE +
            "<set>" +
            "<if test='pcbaDeviceParamType.pcbaDeviceParamTypeName != null and pcbaDeviceParamType.pcbaDeviceParamTypeName != \"\" '>" +
            " pcba_device_param_type_name = #{pcbaDeviceParamType.pcbaDeviceParamTypeName}," +
            "</if>" +
            "<if test='pcbaDeviceParamType.sort != null and pcbaDeviceParamType.sort != \"\" '>" +
            " sort = #{pcbaDeviceParamType.sort}," +
            "</if>" +
            "<if test='pcbaDeviceParamType.pcbaDeviceTypeId != null and pcbaDeviceParamType.pcbaDeviceTypeId != \"\" '>" +
            " pcba_device_type_id = #{pcbaDeviceParamType.pcbaDeviceTypeId}," +
            "</if>" +
            "<if test='pcbaDeviceParamType.status != null and pcbaDeviceParamType.status != \"\" '>" +
            " status = #{pcbaDeviceParamType.status}," +
            "</if>" +
            "</set>" +
            " where pcba_device_param_type_id = #{pcbaDeviceParamType.pcbaDeviceParamTypeId}" +
            "</script>")
    Integer updatePcbaDeviceParamType(@Param("pcbaDeviceParamType") PcbaDeviceParamType pcbaDeviceParamType);

    @ResultMap(value = "pcbaDeviceParamTypeResultMap")
    @Select("select * from " + DB1TableName.PCBA_DEVICE_PARAM_TYPE + " where pcba_device_type_id = #{pcbaDeviceTypeId} and pcba_device_param_type_name = #{pcbaDeviceParamTypeName}")
    PcbaDeviceParamType getByPcbaDeviceTypeIdAndName(@Param("pcbaDeviceTypeId") Integer pcbaDeviceTypeId, @Param("pcbaDeviceParamTypeName") String pcbaDeviceParamTypeName);
}
